#include "prologio.h"

void readln(char * result)
{
	gets (result);
}

void readint(int * result)
{
	scanf("%d",result);
}

void readreal(float * result)
{
	scanf("%f",result);
}

void readchar(char * result)
{
	scanf("%c",result);
}

void readterm(variable * value)
{
	c_type* temp = new c_type;
	if(value->get_vartype()==Real)
		readreal(&(temp->f));
	else if(value->get_vartype()==Integer)
		readint(&(temp->i));
	else if(value->get_vartype()==String)
		readln(temp->s);
	else if(value->get_vartype()==Char)
		readchar(&(temp->c));
	value->set_value(temp);
}

void nl()
{
	printf("\n");
}

bool upper_lower(char * first, char * second)
{			
	bool result =  false;
	if(first!=NULL&&second!=NULL)
	{
		int i = 0;
		char * str = new char[strlen(second)+1];
		strcpy(str,second);
	    while (str[i]) 
		{
		    str[i]=toupper(str[i]);
		    i++;
		}
		str[strlen(second)]='\0';
		result = strcmp(str,first)==0;
	}
	else if(first==NULL&&second!=NULL)
	{
		int i = 0;
		first = new char[strlen(second)];
	    while (second[i]) 
		{
		    first[i]=toupper(second[i]);
		    i++;
		}
		result = true;
	} 
	else if(first!=NULL&&second==NULL) 
	{
		int i = 0;
		second = new char[strlen(first)];
	    while (first[i]) 
		{
		    second[i]=toupper(first[i]);
		    i++;
		}
		result = true;
	}
	return result;
}

bool char_int(char* str,int* i)
{
	if(str!=NULL&&i!=NULL)
	{
		int k=*str;
		return k==*i;
	}
	else if(str!=NULL&&i==NULL)
	{
		 i = new int;
		 *i=*str;	
		 return true;
	}
	else if(str==NULL&&i!=NULL)
	{
		char* str = new char;
		*str=(char)(*i);
		return true;
	}
	return false;
}

bool str_char(char* str,char* i)
{
	if(str!=NULL&&i!=NULL)
	{
		char* str1 = new char[2];
		str1[0]=*i;
		str1[1]='\0';	
		return strcmp(str1,str)==0;
	}
	else if(str!=NULL&&i==NULL)
	{
		 i = new char;
		 *i=str[0];	
		 return true;
	}
	else if(str==NULL&&i!=NULL)
	{
		char* str = new char[2];
		str[0]=*i;
		str[1]='\0';
		return true;
	}
	return false;
}

bool str_real(char* str,float* i)
 {
	if(str!=NULL&&i!=NULL)
	{
		char* str1 = new char[100];
		sprintf(str1,"%f",*i);
		return strcmp(str1,str)==0;
	}
	else if(str!=NULL&&i==NULL)
	{
		 i = new float;
		 *i=atof(str);	
		 return true;
	}
	else if(str==NULL&&i!=NULL)
	{
		char* str = new char[100];
		sprintf(str,"%f",*i);
		return true;
	}
	return false;
}

bool str_int(char* str,int* i)
{
	if(str!=NULL&&i!=NULL)
	{
		char* str1 = new char[100];
		itoa(*i,str1,10);
		return strcmp(str1,str)==0;
	}
	else if(str!=NULL&&i==NULL)
	{
		 i = new int;
		 *i=atoi(str);	
		 return true;
	}
	else if(str==NULL&&i!=NULL)
	{
		char* str = new char[100];
		itoa(*i,str,10);
		return true;
	}
	return false;
}

bool iofunc(QString func,variable* first,variable* second) 
{
	c_type* temp1 = new c_type;
	c_type* temp2 = new c_type;
	temp1 = first->get_value();
	temp2 = second->get_value();
	if(func.compare(QString("nl"))==0)
	{
		nl();
	}
	else if(func.compare(QString("str_int"))==0)
	{
		return str_int(temp1->s,&(temp2->i));
	}
	else if(func.compare(QString("str_real"))==0)
	{
		return str_real(temp1->s,&(temp2->f));
	}
	else if(func.compare(QString("str_char"))==0)
	{
		return str_char(temp1->s,&(temp2->c));
	}
	else if(func.compare(QString("char_int"))==0)
	{
		return char_int(&(temp1->c),&(temp2->i));
	}
	else if(func.compare(QString("upper_lower"))==0)
	{
		return upper_lower(temp1->s,temp2->s);
	}
	else if(func.compare(QString("readint"))==0)
	{
		readint(&(temp1->i));
	}
	else if(func.compare(QString("readreal"))==0)
	{
		readreal(&(temp1->f));
	}
	else if(func.compare(QString("readln"))==0)
	{
		readln(temp1->s);
	}
	else if(func.compare(QString("readchar"))==0)
	{
		readchar(&(temp1->c));
	}
	else if(func.compare(QString("readterm"))==0)
	{
		readterm(first);
		return true;
	}
	else if(func.compare(QString("write"))==0)
	{
		write(first);
		return true;
	}
	first->set_value(temp1);
	second->set_value(temp2);
	return true;
}

void write(variable* var)
{
	if(var->get_vartype()==String)
		puts(var->get_value()->s);
	else if(var->get_vartype()==Char)
		printf("%c",var->get_value()->c);
	else if(var->get_vartype()==Integer)
		printf("%d",var->get_value()->i);
	else if(var->get_vartype()==Real)
		printf("%f",var->get_value()->f);
}